package com.horoscope.astrology.zodiac.signs.free.utils

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import com.horoscope.astrology.zodiac.signs.free.MmoConstant

object ProjectUtils {
    fun goToRating(activity: Activity) {
        val url = MmoConstant.RATING_US_URL
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data = Uri.parse(url)
        val list = activity.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
        if (list.size > 1) {
            list.forEach {
                val packageName = it.activityInfo.packageName
                val className = it.activityInfo.name
                if (null != packageName && packageName.contains("com.android.vending")) {
                    intent.setClassName(packageName, className)
                }
            }
            activity.startActivity(intent)
        } else {
            try {
                activity.startActivity(intent)
            } catch (e: Exception) {
                val viewIntent = Intent(Intent.ACTION_VIEW)
                viewIntent.data = Uri.parse(MmoConstant.URL_RATING_HTTP)
                activity.startActivity(viewIntent)
            }
        }
    }
}